<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业名称" prop="enterprise">
        <el-input
          v-model="queryParams.enterprise"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="status=='1'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:platformServiceOrder:add']"
        >新增</el-button>
      </el-col>
    </el-row>


    <el-tabs v-model="status" type="card" @tab-click="handleClick">
      <el-tab-pane label="待支付" name="1">
        <el-table v-loading="loading" :data="platformServiceOrderList" @selection-change="handleSelectionChange">
          <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
          <el-table-column :formatter="defaultTableColumnDate" label="订单号" align="center" prop="orderNumber" />
          <el-table-column :formatter="defaultTableColumnDate" label="服务事项" align="center" prop="serviceMatters" />
          <el-table-column :formatter="defaultTableColumnDate" label="企业名称" align="center" prop="enterprise" />
          <el-table-column :formatter="defaultTableColumnDate" label="联系人" align="center" prop="userName" />
          <el-table-column :formatter="defaultTableColumnDate" label="手机号码" align="center" prop="phone" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付时间" align="center" prop="payTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :formatter="defaultTableColumnDate" label="服务费用" align="center" prop="serviceFee" />

          <!-- <el-table-column :formatter="defaultTableColumnDate" label="服务说明" align="center" prop="serviceDescription" />
          <el-table-column :formatter="defaultTableColumnDate" label="状态 1 待支付 2 已支付 3 已取消" align="center" prop="status" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付人" align="center" prop="payUser" />
          <el-table-column :formatter="defaultTableColumnDate" label="备注" align="center" prop="remark" /> -->
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformServiceOrder:edit']"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="已支付" name="2">
        <el-table v-loading="loading" :data="platformServiceOrderList" @selection-change="handleSelectionChange">
          <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
          <el-table-column :formatter="defaultTableColumnDate" label="订单号" align="center" prop="orderNumber" />
          <el-table-column :formatter="defaultTableColumnDate" label="服务事项" align="center" prop="serviceMatters" />
          <el-table-column :formatter="defaultTableColumnDate" label="企业名称" align="center" prop="enterprise" />
          <el-table-column :formatter="defaultTableColumnDate" label="联系人" align="center" prop="userName" />
          <el-table-column :formatter="defaultTableColumnDate" label="手机号码" align="center" prop="phone" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付时间" align="center" prop="payTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :formatter="defaultTableColumnDate" label="服务费用" align="center" prop="serviceFee" />

          <!-- <el-table-column :formatter="defaultTableColumnDate" label="服务说明" align="center" prop="serviceDescription" />
          <el-table-column :formatter="defaultTableColumnDate" label="状态 1 待支付 2 已支付 3 已取消" align="center" prop="status" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付人" align="center" prop="payUser" />
          <el-table-column :formatter="defaultTableColumnDate" label="备注" align="center" prop="remark" /> -->
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformServiceOrder:edit']"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-tab-pane>
      <el-tab-pane label="已取消" name="3">
        <el-table v-loading="loading" :data="platformServiceOrderList" @selection-change="handleSelectionChange">
          <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
          <el-table-column :formatter="defaultTableColumnDate" label="订单号" align="center" prop="orderNumber" />
          <el-table-column :formatter="defaultTableColumnDate" label="服务事项" align="center" prop="serviceMatters" />
          <el-table-column :formatter="defaultTableColumnDate" label="企业名称" align="center" prop="enterprise" />
          <el-table-column :formatter="defaultTableColumnDate" label="联系人" align="center" prop="userName" />
          <el-table-column :formatter="defaultTableColumnDate" label="手机号码" align="center" prop="phone" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付时间" align="center" prop="payTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :formatter="defaultTableColumnDate" label="服务费用" align="center" prop="serviceFee" />

          <!-- <el-table-column :formatter="defaultTableColumnDate" label="服务说明" align="center" prop="serviceDescription" />
          <el-table-column :formatter="defaultTableColumnDate" label="状态 1 待支付 2 已支付 3 已取消" align="center" prop="status" />
          <el-table-column :formatter="defaultTableColumnDate" label="支付人" align="center" prop="payUser" />
          <el-table-column :formatter="defaultTableColumnDate" label="备注" align="center" prop="remark" /> -->
          <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:platformServiceOrder:edit']"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台服务订单对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="form.id" label-width="80px">
        <el-form-item label="服务事项" prop="serviceMatters">
          <el-input v-model="form.serviceMatters" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="提交日期" v-if="form.status == '2' || form.status == '3'">
          <el-input v-model="form.payTime" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付日期" v-if="form.status == '2'">
          <el-input v-model="form.createTime" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="服务费用" prop="serviceFee">
          <el-input v-model="form.serviceFee" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="选择企业" prop="enterprise">
          <el-select
            v-model="form.enterprise"
            @change="changeEnterprise"
            placeholder="请选择企业"
            clearable
            style="width: 100%;"
          >
            <el-option
              v-for="item in enterpriseList"
              :key="item.id"
              :label="item.enterpriseName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="服务说明" prop="serviceDescription">
          <el-input v-model="form.serviceDescription" type="textarea" placeholder="请输入内容,至少五个字符" />
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="form.userName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlatformServiceOrder, getPlatformServiceOrder, delPlatformServiceOrder, addPlatformServiceOrder, updatePlatformServiceOrder, enterpriseInfoList } from "@/api/system/platformServiceOrder";

export default {
  name: "PlatformServiceOrder",
  data() {
    return {
      enterpriseList: [],
      status: '1',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台服务订单表格数据
      platformServiceOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        serviceMatters: null,
        serviceFee: null,
        enterprise: null,
        serviceDescription: null,
        userName: null,
        phone: null,
        status: null,
        payUser: null,
        payTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceMatters: [
          { required: true, trigger: "blur", message: "请输入服务事项" }
        ],
        serviceFee: [
          { required: true, trigger: "blur", message: "请输入服务费用" }
        ],
        enterprise: [
          { required: true, trigger: "change", message: "请选择企业" }
        ],
        serviceDescription: [
          { required: true, trigger: "blur", message: "请输入服务说明" },
          { min: 5, message: '至少五个字符', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getEnterpriseInfoList()
  },
  methods: {
    changeEnterprise(e) {
      const info = this.enterpriseList.find(v => v.id == e)
      this.form.userName = info.contacts || '-'
      this.form.phone = info.phone
    },
    getEnterpriseInfoList() {
      enterpriseInfoList({ }).then(response => {
        this.enterpriseList = response.rows
      });
    },
    handleClick(tab, event) {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询平台服务订单列表 */
    getList() {
      this.loading = true;
      listPlatformServiceOrder({...this.queryParams, status: this.status}).then(response => {
        this.platformServiceOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        serviceMatters: null,
        serviceFee: null,
        enterprise: null,
        serviceDescription: null,
        userName: null,
        phone: null,
        status: null,
        payUser: null,
        payTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台服务订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlatformServiceOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台服务订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.open = false;
          } else {
            addPlatformServiceOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除平台服务订单编号为"' + ids + '"的数据项？').then(function() {
        return delPlatformServiceOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/platformServiceOrder/export', {
        ...this.queryParams
      }, `platformServiceOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
